package com.ipharma.gui;

import javax.swing.ImageIcon;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.dto.Product;

/**
 * A Swing-based panel class.
 * <P>
 * 
 * @author JorgE.H
 */
public class GUIButtonProduct extends GUIButtonImage {

   /**
    * Panel
    */

   /**
    * Attributes
    */

   Product product;

   boolean active = true;

   boolean available;

   int position;

   ImageIcon buttDisabled;

   /**
    * Constructs a new instance.
    * 
    * @param pProd
    *           the Product to add
    * @param pActive
    *           boolean value to set active button state
    * @param pPosition
    *           position index in panel (vector) of products
    */
   public GUIButtonProduct(Product pProd, boolean pActive, int pPosition,
         ImageIcon pUp, ImageIcon pDown) {

      super(pUp, pDown);
      try {
         buttDisabled = pUp;
         position = pPosition;
         active = pActive;
         product = pProd;
         jbInit();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Constructs a new instance.
    * 
    * @param pLabel
    *           string to show as product label
    * @param pQty
    *           stock available for product
    * @param pCode
    *           string product code
    * @param pActive
    *           boolean value to set active button state
    * @param pHighlight
    *           boolean value to indicate product availability
    * @param pPosition
    *           position index in panel (vector) of products
    */
   public GUIButtonProduct(String pLabel, int pQty, String pCode,
         boolean pActive, boolean pHighlight, int pPosition, ImageIcon pUp,
         ImageIcon pDown) {

      super(pUp, pDown);
      try {
         buttDisabled = pUp;
         position = pPosition;
         active = pActive;
         product = new Product( pLabel, pQty, pCode, pHighlight );
         jbInit();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Initializes the state of this instance.
    */
   private void jbInit() throws Exception {

      available = product.isAvailable();
      setText(getExtLabel());
      setRequestFocusEnabled(false);
      setDisabledIcon(buttDisabled);

      if (product.getQuantity() == 0)
         setEnabled(false);

   }

   /**
    * Sets button text
    * 
    * @param pDesc
    *           product description
    * @param pQty
    *           product quantity
    */
   private String getExtLabel() {

      if (product.getDescription().equals(McdPrs.EMPTY))
         return "";
      int li_text = Colors.APN_HT_TEXT;
      if (!available)
         li_text = Colors.APN_HT_TEXT_DIS;
      return "<html><b><font size='2' face='Verdana, Helvetica, sans-serif' color='#"
            + Integer.toHexString(li_text)
            + "'>"
            + "<p align='center'>"
            + product.getDescription()
            + "</p><p align='center'>"
            + product.getQuantity() + "</p></font></b></html>";

   }

   /**
    * Sets button label
    * 
    * @param pActive
    *           boolean for button initial state
    */
   public void setProduct(Product pProd) {

      product.copy(pProd);
      setText(getExtLabel());
      if (pProd.getQuantity() == 0)
         setEnabled(false);
   }

   /**
    * Sets button active
    * 
    * @param pActive
    *           boolean for button initial state
    */
   public void setActive(boolean pActive) {

      setEnabled(pActive);
   }
}
